from pyscf import gto, scf, lib
import numpy as np

def cre_coord(coord,atom,basis):
    mol=gto.Mole()
    mol.atom=atom
    mol.basis = basis
    mol.verbose = 3
    mol.build()
    print("传入的坐标：", coord)
    mol.set_geom_(coord * lib.param.BOHR)
    print("更新后的坐标：", mol.atom_coords())  # 打印更新后的坐标
    return mol.build()

def opt_geom(coord_orig,atom,basis):
    coord = coord_orig.copy()
    lr = 1
    eng_threshold = 1e-8
    eng_list = []
    for _ in range(200):
        scf_eng = scf.RHF(cre_coord(coord,atom,basis)).run()
        eng_list.append(scf_eng.e_tot)
        if len(eng_list) > 6 and np.abs(eng_list[-1] - eng_list[-4]) < eng_threshold:
            break
        grad_mo = scf_eng.nuc_grad_method().run().de
        coord -= lr * grad_mo
    print('ok opt geom')
    return coord
